package com.yidu.qianfeng.controller;

import com.yidu.qianfeng.common.bean.Vehicle.CarInfo;
import com.yidu.qianfeng.service.impl.VehicleServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆管理控制器
 */
@Controller("vehicleController")//标注是控制器层
public class VehicleController {
    //加载车辆列表页面
    @RequestMapping("/goVehicleList")
    public String goVehicleList(){
        return "views/vehicleList";
    }
    //加载车辆列表页面
    @RequestMapping("/loadVehicle")
    public String loadVehicle(){
        return "views/addvehicle";
    }
    //加载车辆列表页面
    @RequestMapping("/goUpdateVehicle")
    public String goUpdateVehicle(){
        return "views/updatevehicle";
    }

    @Resource(name = "vehicleServiceImpl")//依赖注入服务层实现类
    private VehicleServiceImpl vehicleService;

    /**
     * 分页查询车辆信息
     * @param page 页面
     * @param limit 页目
     * @return map集合
     */
    @RequestMapping("/selectCarInfoPaing")
    public @ResponseBody Map
    selectCarInfoPaing(int page,int limit){
        List<CarInfo> carInfos = vehicleService.selectCarInfoPaing((page - 1) * limit, limit);//分页集合
        Integer count = vehicleService.selectCarInfoCount();//分页集合个数

        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",carInfos);

        return map;
    }

    @RequestMapping("/queryCarsBySubst")
    public @ResponseBody
    List<CarInfo> queryCarBySubst(String carPartofSubs){
        return vehicleService.queryCarBySubst(carPartofSubs);
    }

    /**
     * 动态sql查询车辆信息
     * @param carInfo 车辆信息对象
     * @return map集合
     */
    @RequestMapping("/dynamicSqlQuery")
    public @ResponseBody Map
    dynamicSqlQuery(CarInfo carInfo){
        List<CarInfo> carInfos = vehicleService.dynamicSqlQuery(carInfo);//车辆信息表集合
        int size = vehicleService.dynamicSqlQuery(carInfo).size();//车辆信息表集合的个数

        Map map = new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",size);
        map.put("data",carInfos);

        return map;
    }

    /**
     * 新增车辆信息表
     * @param carInfo 车辆信息对象
     * @return 结果数据
     */
    @RequestMapping("/addVehicle")
    public @ResponseBody String
    addVehicle(CarInfo carInfo){
        System.out.println(carInfo.toString());

        return vehicleService.addVehicle(carInfo);
    }

    /**
     * 删除车辆信息表数据
     * @param carNumber 主键id
     * @return 结果信息
     */
    @RequestMapping("/deleteVehicleDate")
    public @ResponseBody String
    deleteVehicleDate(String carNumber){

        return vehicleService.deleteVehicleDate(carNumber);
    }

    /**
     * 根据主键id查询车辆信息表数据
     * @param carNumber 主键id
     * @return 结果对象
     */
    @RequestMapping("/byIdQueryVehicle")
    public @ResponseBody CarInfo
    byIdQueryVehicle(String carNumber){


        return vehicleService.byIdQueryVehicle(carNumber);
    }

    /**
     * 修改车辆信息表数据
     * @param carInfo 车辆信息表对象
     * @return 结果信息
     */
    @RequestMapping("/updateVehicle")
    public @ResponseBody String
    updateVehicle(CarInfo carInfo){

        return vehicleService.updateVehicle(carInfo);
    }
}
